#!/usr/bin/env perl
use warnings;
use strict;

use DBI;

# database configuration
use constant DB_DBI  => 'dbi:mysql:mydatabase';
use constant DB_USER => 'myuser';
use constant DB_PASS => 'mypassword';

# database user permissions, do not change
use constant PERM_VIEW => 1;
use constant PERM_MOD  => 2;

sub get_user($$) {
    my ($dbh, $ident_hash) = @_;

    my %user = (
        id          => 0,
        name        => undef,
        ident_hash  => undef,
        permissions => 0
    );

    my $sth = $dbh->prepare("SELECT * from users WHERE ident_hash = ?");
    $sth->execute($ident_hash);

    while (my $result = $sth->fetchrow_hashref()) {
        $user{'id'} = $result->{'id'};
        $user{'name'} = $result->{'name'};
        $user{'ident_hash'} = $result->{'ident_hash'};
        $user{'permissions'} = $result->{'permissions'};
    }
    $sth->finish();

    return %user;
}

1;
